﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Cstudy.DAL
{
    public class Artcles
    {
        public bool AddArtcle(MODEL.artcles artcle)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@title",artcle.Title),
                new SqlParameter("@content",artcle.Content),
                new SqlParameter("@summary",artcle.Summary),
                new SqlParameter("@authorid",artcle.AuthorID),
                new SqlParameter("@classid",artcle.ClassID),
                new SqlParameter("@tagid",artcle.TagID),
                new SqlParameter("@states",(int)artcle.States)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_AddArtcle", parms);
        }

        public bool EditArtcle(MODEL.artcles artcle)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@postid",artcle.postID),
                new SqlParameter("@title",artcle.Title),
                new SqlParameter("@content",artcle.Content),
                new SqlParameter("@summary",artcle.Summary),
                new SqlParameter("@authorid",artcle.AuthorID),
                new SqlParameter("@classid",artcle.ClassID),
                new SqlParameter("@tagid",artcle.TagID),
                new SqlParameter("@states",artcle.States)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_EditArtcle", parms);
        }

        public DataSet SelectArtcleByID(int id)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@postid",id)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectArtcleByID", parms);
        }

        public bool ArtcleAddReadtime(int id)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@postid",id)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_AddArtcleReadtime",parms);
        }

        public DataSet SearchArtcleTitle(string keyword)
        {
            keyword = "%" + keyword.Replace(" ", "%") + "%";
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@keyword",keyword)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SearchArtcleTitle", parms);
        }

        public DataSet SearchArtcleContent(string keyword)
        {
            keyword = "%" + keyword.Replace(" ", "%") + "%";
            SqlParameter[] parms = new SqlParameter[]{
                new SqlParameter("@keyword",keyword)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SearchArtcleContent", parms);
        }

        public bool DeleteArtcleByID(int id)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@postid",id)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_DeleteArtcleByID", parms);
        }

        public DataSet SelectArtcleByClassID(int classid)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@classid",classid)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectArtcleByClassID", parms);
        }

        public DataSet SelectArtcleByClassID(int classid, int currentPage, int pagesize, out int num, string returnstring)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@classid",classid),
                new SqlParameter(returnstring,SqlDbType.Int)
            };
            parms[1].Direction = ParameterDirection.Output;
            return COM.SQLTools.ExecuteQuery("sp_SelectArtcleByClassID", parms, currentPage, pagesize, out num, returnstring);
        }

        public DataSet SelectArtcleByClassIDWithCount(int classid,int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@classid",classid),
                new SqlParameter("@count",count)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectArticleByClassIDWithCount", parms);
        }

        public DataSet SelectArtcleByTagIDWithCount(int tagid, int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@tagid",tagid),
                new SqlParameter("@count",count)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectArticleByTagIDWithCount", parms);
        }

        public DataSet SelectArtcle()
        {
            return COM.SQLHelper.ExecuteQuery("sp_SelectArtcle");
        }

        public DataSet SelectArtcle(int currentPage, int pagesize, out int num, string returnstring)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@count",SqlDbType.Int)
            };
            parms[0].Direction = ParameterDirection.Output;

            return COM.SQLTools.ExecuteQuery("sp_SelectArtcle", parms, currentPage, pagesize, out num, returnstring);
        }

        public DataSet SelectNewArtcle(int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@top",count)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectNewArticle", parms);
        }

        public DataSet SelectHotArticle(int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@top",count)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectHotArticle", parms);
        }
    }
}
